﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Ultima.Xna.Graphics.UI.Gumps
{
    public class GumpBackground : GumpEntry
    {
        private int _gumpId;

        private Texture2D _topLeftTexture;
        private Texture2D _topTexture;
        private Texture2D _topRightTexture;
        private Texture2D _middleLeftTexture;
        private Texture2D _middleTexture;
        private Texture2D _middleRightTexture;
        private Texture2D _bottomLeftTexture;
        private Texture2D _bottomTexture;
        private Texture2D _bottomRightTexture;

        public override bool Moveable
        {
            get
            {
                if (Parent != null && Parent is Gump && Parent.Moveable)
                {
                    return true;
                }

                return base.Moveable;
            }
            set
            {
                base.Moveable = value;
            }
        }

        public Texture2D TopLeftTexture
        {
            get { return _topLeftTexture; }
        }

        public Texture2D TopTexture
        {
            get { return _topTexture; }
        }

        public Texture2D TopRightTexture
        {
            get { return _topRightTexture; }
        }

        public Texture2D MiddleLeftTexture
        {
            get { return _middleLeftTexture; }
        }

        public Texture2D MiddleTexture
        {
            get { return _middleTexture; }
        }

        public Texture2D MiddleRightTexture
        {
            get { return _middleRightTexture; }
        }

        public Texture2D BottomLeftTexture
        {
            get { return _bottomLeftTexture; }
        }

        public Texture2D BottomTexture
        {
            get { return _bottomTexture; }
        }

        public Texture2D BottomRightTexture
        {
            get { return _bottomRightTexture; }
        }

        public int GumpId
        {
            get { return _gumpId; }
            set 
            {
                _gumpId = value; 
                InitializeTextures();
            }
        }

        public GumpBackground(Game game)
            : base(game)
        {

        }

        public override void Initialize()
        {
            base.Initialize();

            InitializeTextures();
        }

        private void InitializeTextures()
        {
            _topLeftTexture = Data.Gumps.CreateTexture(Game.GraphicsDevice, _gumpId);
            _topTexture = Data.Gumps.CreateTexture(Game.GraphicsDevice, _gumpId + 1);
            _topRightTexture = Data.Gumps.CreateTexture(Game.GraphicsDevice, _gumpId + 2);
            _middleLeftTexture = Data.Gumps.CreateTexture(Game.GraphicsDevice, _gumpId + 3);
            _middleTexture = Data.Gumps.CreateTexture(Game.GraphicsDevice, _gumpId + 4);
            _middleRightTexture = Data.Gumps.CreateTexture(Game.GraphicsDevice, _gumpId + 5);
            _bottomLeftTexture = Data.Gumps.CreateTexture(Game.GraphicsDevice, _gumpId + 6);
            _bottomTexture = Data.Gumps.CreateTexture(Game.GraphicsDevice, _gumpId + 7);
            _bottomRightTexture = Data.Gumps.CreateTexture(Game.GraphicsDevice, _gumpId + 8);
        }

        public override void Draw(GameTime gameTime)
        {
            Manager.Renderer.Render(this);

            //Vector2 position = Position;
            //int x = (int)position.X;
            //int y = (int)position.Y;
            //int leftWidth = _middleLeftTexture.Width;
            //int rightWidth = _middleRightTexture.Width;
            //int topHeight = _topTexture.Height;
            //int bottomHeight = _bottomTexture.Height;
            //int width = (int)Size.X;
            //int height = (int)Size.Y;

            //Rectangle[] sourceRectangles = new Rectangle[] {
            //    new Rectangle(0, 0, leftWidth - 2, topHeight),//topleft
            //    new Rectangle(0, 0, width - (rightWidth + leftWidth) + 4, topHeight),//topmiddle
            //    new Rectangle(0, 0, rightWidth - 1, topHeight),//topright
            //    new Rectangle(0, 0, leftWidth, height - (topHeight + bottomHeight)),//midleft
            //    new Rectangle(0, 0, width - (leftWidth + rightWidth) + 4, height - (topHeight + bottomHeight)),//midmid
            //    new Rectangle(0, 0, rightWidth, height - (topHeight + bottomHeight)),//midright
            //    new Rectangle(0, 0, leftWidth - 2, bottomHeight),//botleft
            //    new Rectangle(0, 0, width - (leftWidth + rightWidth) + 4, bottomHeight),//botmiddle
            //    new Rectangle(0, 0, rightWidth - 1, bottomHeight)//botright
            //};

            ////End the spritebatch 
            ////incase its already started.
            //Manager.SpriteBatch.End();
            //Manager.SpriteBatch.Shader = GumpEffect;
            //Manager.SpriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);

            // //Topleft
            //Manager.SpriteBatch.Draw(_topLeftTexture, position, sourceRectangles[0], Color.White);
            ////Top
            //Manager.SpriteBatch.Draw(_topTexture, position + new Vector2(leftWidth - 2, 0), sourceRectangles[1], Color.White);
            ////Topright
            //Manager.SpriteBatch.Draw(_topRightTexture, position + new Vector2(width - (rightWidth - 2), 0), sourceRectangles[2], Color.White);
            ////Left
            //Manager.SpriteBatch.Draw(_middleLeftTexture, position + new Vector2(0, topHeight), sourceRectangles[3], Color.White);
            ////Middle
            //Manager.SpriteBatch.Draw(_middleTexture, position + new Vector2(leftWidth, topHeight), sourceRectangles[4], Color.White);
            ////Right
            //Manager.SpriteBatch.Draw(_middleRightTexture, position + new Vector2(width - rightWidth + 2, topHeight), sourceRectangles[5], Color.White);
            ////Bottomleft
            //Manager.SpriteBatch.Draw(_bottomLeftTexture, position + new Vector2(0, height - bottomHeight), sourceRectangles[6], Color.White);
            ////Bottom
            //Manager.SpriteBatch.Draw(_bottomTexture, position + new Vector2(leftWidth - 2, height - bottomHeight), sourceRectangles[7], Color.White);
            ////Bottomright
            //Manager.SpriteBatch.Draw(_bottomRightTexture, position + new Vector2(width - (rightWidth - 2), height - bottomHeight), sourceRectangles[8], Color.White);

            //Manager.SpriteBatch.End(true);

            ////Restart the spritebatch 
            //Manager.SpriteBatch.Begin();

            base.Draw(gameTime);            
        }
    }
}
